// pages/owner/owner.js
const app = getApp()
Page({

  data: {
    function_btn: [
      {
        icon: 'icon-wode1',
        text: '我的记录',
        function: 0
      },
      {
        icon: 'icon-shequ-active',
        text: '信息公告',
        function: 1
      },
      {
        icon: 'icon-xunhuan',
        text: '投票决议',
        function: 2
      },
      {
        icon: 'icon-classrichangqingjie',
        text: '业委会活动',
        function: 3
      }
    ],
    // owner_card_list: [],
    owner_card_list: [
    ],
    type_list: [
      {
        type_name: '信息公告',
        icon_url: 'https://z3.ax1x.com/2021/07/05/RhmAdx.png'
      },
      {
        type_name: '投票决议',
        icon_url: 'https://z3.ax1x.com/2021/07/05/RhmZFK.png'
      },
      {
        type_name: '业委会活动',
        icon_url: 'https://z3.ax1x.com/2021/07/05/RhmEo6.png'
      },
    ],
    show_card_list: [
    ],
    scroll_height: 0,
    showActivities: true
  },

  // 获取我的记录
  getMyRecord: function (e){
    app.sendGetRequest('/community/owner/activity/me', (res) => {
      // console.log('我的记录', res.data)
      let data = res.data.sort((a, b) =>{
        var t1 = new Date(a.time.substring(0,19).replace(/-/g,'/')).getTime()
        var t2 = new Date(b.time.substring(0,19).replace(/-/g,'/')).getTime()
        return t2-t1
      });
      this.setData({
        record_list: data
      })
    })
  },

  onFunctionTap: function (e){
    var type = e.currentTarget.dataset.function
    if(type==0){
      // wx.navigateTo({
      //   url: './me/me',
      // })
      this.getMyRecord()
      this.setData({
        showActivities: false
      })
    }else{
      var temp_list = []
      for (var i=0; i<this.data.owner_card_list.length; i++){
        var card = this.data.owner_card_list[i]
        if(card.type == type - 1){
          temp_list.push(card)
        }
      }
      this.setData({
        show_card_list: temp_list,
        showActivities: true
      })
    }
  },
  onCardTap: function(e){
    var type = e.currentTarget.dataset.card_id
    var id = e.currentTarget.dataset.id
    if(type==0){
      wx.navigateTo({
        url: './noticeDetail/noticeDetail?id=' + id,
      })
    }else if(type==1){
      wx.navigateTo({
        url: './voteDetail/voteDetail?id=' + id,
      })
    }else if(type==2){
      wx.navigateTo({
        url: './activityDetail/activityDetail?id=' + id,
      })
    }
  },
  onRecordTap: function(e) {
    var type = e.currentTarget.dataset.type
    var id = e.currentTarget.dataset.id
    if(type==0){
      wx.navigateTo({
        url: './voteDetail/voteDetail?id=' + id,
      })
    }else{
      wx.navigateTo({
        url: './activityDetail/activityDetail?id=' + id,
      })
    }
  },

  onLoad: function (options) {
    this.setData({
      showActivities: true
    })
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scroll_height: windowHeight * 750 / windowWidth - 450 - 30
    })
    app.sendGetRequest('/community/owner/preview/', (res) => {
      // console.log('返回业委会公告数据', res)
      res = res.sort((a, b) =>{
        var t1 = new Date(a.time.substring(0,19).replace(/-/g,'/')).getTime()
        var t2 = new Date(b.time.substring(0,19).replace(/-/g,'/')).getTime()
        return t2-t1
      });
      // console.log('业委会排序', res)
      this.setData({
        owner_card_list: res,
        show_card_list: res
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  onReady: function () {

  },
})